package com.library.picture.books.service.impl;

import com.alibaba.fastjson.JSON;
import com.library.common.utils.StringUtils;
import com.library.picture.books.entity.PictureType;
import com.library.picture.books.service.IPictureTypeService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 图书分类
 *
 * @author wanghongjie
 */
@Slf4j
@Service
public class PictureTypeServiceImpl implements IPictureTypeService {
    @Resource
    private MongoTemplate mongoTemplate;

    public static final String COLLECTION_NAME = "picture_type";

    @Override
    public PictureType selectPictureTypeById(String id) {
        return mongoTemplate.findById(id, PictureType.class, COLLECTION_NAME);
    }

    @Override
    public List<PictureType> selectPictureTypeList(PictureType pictureType, Pageable pageable) {
        Query query = new Query();
        if (StringUtils.isNotEmpty(pictureType.getTypeNo())) {
            query.addCriteria(Criteria.where("typeNo").is(pictureType.getTypeNo()));
        }
        if (StringUtils.isNotEmpty(pictureType.getParentTypeNo())) {
            query.addCriteria(Criteria.where("parentTypeNo").is(pictureType.getParentTypeNo()));
        }
        if (StringUtils.isNotEmpty(pictureType.getTypeName())) {
            Pattern pattern = Pattern.compile("^.*" + pictureType.getTypeName() + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("typeName").regex(pattern));
        }
        pictureType.setTotal(mongoTemplate.count(query, PictureType.class, COLLECTION_NAME));
        query.with(pageable);
        return mongoTemplate.find(query, PictureType.class, COLLECTION_NAME);
    }

    @Override
    public int insertPictureType(PictureType pictureType) {
        pictureType.setCreateTime(new Date());
        pictureType.setUpdateTime(new Date());
        PictureType save = mongoTemplate.save(pictureType, COLLECTION_NAME);
        log.info("insertPictureType is {}", JSON.toJSONString(save));
        return 1;
    }

    @Override
    public int updatePictureType(PictureType pictureType) {
        Query query = new Query();
        query.addCriteria(Criteria.where("typeNo").is(pictureType.getTypeNo()));
        Update update = new Update();
        if (StringUtils.isNotEmpty(pictureType.getTypeName())) {
            update.set("typeName", pictureType.getTypeName());
        }
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, COLLECTION_NAME);
        log.info("updateResult is {}", JSON.toJSONString(updateResult));
        return (int) updateResult.getMatchedCount();
    }

    @Override
    public int deletePictureTypeByIds(String[] ids) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(ids));
        DeleteResult remove = mongoTemplate.remove(query, PictureType.class, COLLECTION_NAME);
        return (int) remove.getDeletedCount();
    }

    @Override
    public int deletePictureTypeById(String id) {
        PictureType byId = mongoTemplate.findById(id, PictureType.class);
        if (Objects.nonNull(byId)) {
            DeleteResult remove = mongoTemplate.remove(byId, COLLECTION_NAME);
            return (int) remove.getDeletedCount();
        }
        return 0;
    }
}
